#include <stdio.h>
#include <string.h>
typedef struct student
{
    char name[20];
    int age;
    int score[5];
} stu;

// 结构体赋值
void struct_1()
{
    stu s = {"张三",
             18,
             {100, 99, 98, 97, 96}};
    printf("姓名：%s  年龄：%d\n", s.name, s.age);
    printf("成绩：\t");
    for (size_t i = 0; i < 5; i++)
    {
        printf("%d\t", s.score[i]);
    }
    printf("\n");
}

// 结构体数组
void struct_2()
{
    stu s[3] = {
        {"张三", 18, {100, 99, 98, 97, 96}},
        {.name = "李四",
         .age = 19,
         .score[0] = 100,
         .score[1] = 100,
         .score[2] = 100,
         .score[3] = 100,
         .score[4] = 100},
        {.name = "wangwu", .age = 18, .score = {100, 99, 98, 97, 96}}};
    for (size_t i = 0; i < 3; i++)
    {
        printf("姓名：%s  年龄：%d\n", s[i].name, s[i].age);
        printf("成绩：\t");
        for (size_t j = 0; j < 5; j++)
        {
            printf("%d\t", s[i].score[j]);
        }
        printf("\n");
    }
}

// 结构体指针
void struct_3()
{
    stu s = {"张三",
             18,
             {100, 99, 98, 97, 96}};
    stu *p = &s;
    printf("姓名：%s  年龄：%d\n", (*p).name, (*p).age);
    printf("姓名：%s  年龄：%d\n", p->name, p->age);
    printf("成绩：\t");
    for (size_t i = 0; i < 5; i++)
    {
        printf("%d\t", p->score[i]);
    }
    printf("\n");
}
// 结构体变量相互赋值
void struct_4()
{
    stu s1 = {"张三",
              18,
              {100, 99, 98, 97, 96}};
    printf("请输入名字");
    scanf("%s", s1.name);
    printf("请输入年龄");
    scanf(" %d", &s1.age);
    printf("姓名：%s  年龄：%d\n", s1.name, s1.age);
    printf("成绩：\t");
    for (size_t i = 0; i < 5; i++)
    {
        printf("%d\t", s1.score[i]);
    }
    printf("\n");
}

// 结构体嵌套
void struct_5()
{
    typedef struct birthday
    {
        int Y;
        int M;
        int D;
    } birth;
    typedef struct student
    {
        char name[20];
        int age;
        birth birthday;
    } stu;
    typedef struct student2
    {
        char name[20];
        int age;
        struct birthday birthday;
    } stu2;
    stu s = {"张三",
             18,
             {.Y = 2000, .M = 1, .D = 1}};
    printf("姓名：%s  年龄：%d 年：%d 月：%d 日：%d\n", s.name, s.age, s.birthday.Y, s.birthday.M, s.birthday.D);
}
// 结构体内存分布
void struct_6()
{
    typedef struct student
    {
        char name[20];   // 24 这里要是8的倍数
        double score[5]; // 5 * 8 = 40
        char sex[4];     // 4 会补齐最大类型单位8
        char a;
    } stu;
    stu stuValue = {"张三", {100, 99, 98, 97}, "男"};
    printf("%lu\n", strlen(stuValue.sex));
    // printf("%p,%p,%p", stuValue.name, stuValue.score, stuValue.sex);
    printf("sizeof(stu) = %lu\n", sizeof(stuValue));
}
void main()
{
    struct_6();
}